package com.sen.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.dao.SellDao;
import com.sen.dao.SellheadDao;
import com.sen.dao.VipDao;
import com.sen.entity.ActivityEntity;
import com.sen.entity.DruginfoEntity;
import com.sen.entity.SellEntity;
import com.sen.entity.VipEntity;
import com.sen.service.SellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("sellService")
public class SellServiceImpl extends ServiceImpl<SellDao, SellEntity> implements SellService {

    @Autowired
    SellDao sellDao;
    @Autowired
    SellheadDao sellheadDao;
    @Autowired
    VipDao vipDao;

    @Override
    public Double getmoney(Double money, String state) {
        //查询当前时间是否有活动中
        ActivityEntity activity = sellDao.getactivity();
        //判断是否有活动如果没有则判断是否为会员日
        if (activity != null) {
            //判断是否达到消费最低标准
            if (money > activity.getAmoney()) {
                System.out.println(money);
                System.out.println(activity.getDiscount());
                System.out.println(money * activity.getDiscount());
                money = money * activity.getDiscount();
                return money;
            }
            //没有达到直接返回钱
            else {
                return money;
            }
        }
        //查询该会员是否存在
        if ("会员手机号不可用".equals(state)) {
            //不符合所有条件直接返回
            return money;
        }
        //存在则查询
        else {
            //获取当前日期
            String dd = LocalDate.now().format(DateTimeFormatter.ofPattern("dd"));
            //获取会员日信息可用缓存
            VipEntity vipEntity = vipDao.selectById("1");
            //判断是否为会员日
            if (dd.equals(vipEntity.getDay())) {
                money = money * (vipEntity.getDiscount() / 100);
                return money;
            }
        }
        //以上所有条件不满足
        return money;
    }

    @Override
    public void addOrder(String sid,List<DruginfoEntity> drugs) {
        for (DruginfoEntity drug : drugs) {
            sellDao.addOrder(sid,drug);
        }
    }

    @Override
    public void addOrderHeader(String uuid,String eid, String money,String uid) {
        sellheadDao.addOrder(uuid,eid, money,uid);
    }

}
